import re


class EsUtil(object):
    PAY_LOAD = {
        "_source": ["raw", "row_time"],
        "query": {
            "bool": {
                "filter": [
                ]
            }
        },
        "sort": [
            {
                "row_time": {
                    "order": "asc"
                }
            }
        ],
        "size": 1000
    }

    @staticmethod
    def sql_cluster_decoder(sql):
        sql = sql + " "
        result = {}
        regex_arr = ["(\\s+|\\*?)(from|FROM)\\s+(\\w+\\.)?(\\w+\\.)*(\\w+)(\\)*)\\s+(.|\\s)*?\\w*",
                     "(\\s+)(join|JOIN)\\s+(\\w+\\.)?(\\w+\\.)*(\\w+)\\s+(.|\\s)*?(on|ON)"]
        for regex in regex_arr:
            pattern = re.compile(regex)
            matcher = pattern.findall(sql)
            if matcher:
                for mat in matcher:
                    table = mat[4]
                    cluster = mat[2]
                    if cluster and cluster != '':
                        cluster = cluster[:len(cluster)-1]
                    if cluster not in result.keys():
                        tables = [table]
                        result[cluster] = tables
                    else:
                        tables = result[cluster]
                        tables.append(table)

        return result







